package com.tywj.panda.driver

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.tywj.panda.customer.common.view.base.MyBaseFragment
import com.tywj.panda.customer.common.view.paging.BasePagingFragmentSearch
import com.tywj.panda.driver.api.SelectedDateDialog
import com.tywj.panda.driver.event.DriverEvent
import com.tywj.panda.driver.route.DriverRoute
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.TimeUtil
import com.tywj.lib.route.RouteParam
import kotlinx.android.synthetic.main.driver_fragment_home.*


/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/06/20
 *     desc   : 首页
 *     version: 1.0.0
 * </pre>
 */
@Suppress("DUPLICATE_LABEL_IN_WHEN")
@Route(path = DriverRoute.DRIVER_HOME)
class DriverHomeFragment : MyBaseFragment() {


//    状态 1.待发车 2.进行中3.已完成。

    companion object {
        const val TASK_NO_FINISH = "0"
        const val TASK_HAD_DONE = "1"

        const val PARAM_CONDITION = "condition"
    }

    //我的订单记录类型页面集合
    private val fragmentList = ArrayList<BasePagingFragmentSearch<*, *>>()

    //我的订单记录类型标题数组
    private val titleArray = arrayOfNulls<String>(2)

    private var condition = "0"

    //从订单详情页回来
    private var isBack = false

    private var selectedData = ""

    /**
     * 选择日期
     */
    private val selectedDialog by lazy { initSelectedBuyDialog() }


    override fun getLayoutID(): Int = R.layout.driver_fragment_home

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        condition = RouteParam(this).getString(PARAM_CONDITION)

        titleArray[0] = "待完成"
        titleArray[1] = "已完成"

        fragmentList.add(DriverRoute.getDriverTaskFragment(requireActivity(), TASK_NO_FINISH))
        fragmentList.add(DriverRoute.getDriverTaskFragment(requireActivity(), TASK_HAD_DONE))
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        slidingTabLayout.setViewPager(taskViewPager, titleArray, requireActivity(), fragmentList as ArrayList<Fragment>)

        selectedData = TimeUtil.millis2String(System.currentTimeMillis(), TimeUtil.PATTERN_FORMAT)

        tvSelectedData.text = selectedData

        when (condition) {
            TASK_NO_FINISH -> taskViewPager.currentItem = 0
            TASK_HAD_DONE -> taskViewPager.currentItem = 1
        }
    }

    override fun initListener() {
        super.initListener()

        //查看绩效
        tvPerformance.onClick {
            DriverRoute.toPerformance().navigation(this)
        }


        tvMoreTask.onClick {
            selectedDialog.showDialog(getFragmentManagerInstance())
        }

        selectedDialog.setSelectedDateTime {
//            tvSelectedData.text = it

//            DriverEvent.loadTaskByCurrentDate.post(it)

            //到另一个界面，状态传为2
            DriverRoute.toMoreTask(it).navigation(this)
        }
    }

    override fun onVisibleEveryTime() {
        super.onVisibleEveryTime()

        // 从记录详情页回来或用户切换应用到本App，都需要刷新一次数据
        if (isBack) {
            fragmentList.forEach {
                if (it.isInitialized()) {
                    it.refreshData()
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        isBack = true
    }

    private fun initSelectedBuyDialog(): SelectedDateDialog {
        //as？如果不兼容 会返回为null  ?:为空时会初始化
        return getFragmentManagerInstance().findFragmentByTag(SelectedDateDialog.TAG) as? SelectedDateDialog
            ?: SelectedDateDialog()
    }

}